package cn.anquing.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MQSender {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send(String message){
        logger.info("send message:{}",message);
        amqpTemplate.convertAndSend(MQConfig.QUEUE,message);
    }

    public void sendTopic(String message){
        logger.info("send topic message:{}",message);
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key1",message+":1");
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key2",message+":2");
    }

    public void sendFanout(String message){
        logger.info("send fanout message:{}",message);
        //routingKey为空
        amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"",message);
    }
    public void sendHeader(String message){
        logger.info("send header message:{}",message);
//        map.put("header1", "value1");
//        map.put("header2", "value2");
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("header1","value1");
        messageProperties.setHeader("header2","value2");
        Message obj = new Message(message.getBytes(),messageProperties);
        //routingKey为空
        amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE,MQConfig.HEADER_QUEUE,obj);
    }
}


